#include "GameManager.h"

namespace PeanutQuest {

	GameManager::GameManager() : cg::Entity("GameManager"){
	}
	GameManager::~GameManager() {
	}

	void GameManager::init() {
		v = (cg::View*)cg::Registry::instance()->get("view");
		actualLevel = MENU;
		LoadGameMenu();
	}

	void GameManager::update(unsigned long elapsed_millis) {
		_peanut = (MyPeanut*)cg::Registry::instance()->get("Peanut");
		_sm = (ShooterManager*)cg::Registry::instance()->get("ShooterManager");
		_cm = (CollectableManager*)cg::Registry::instance()->get("CollectableManager");
		_hud = (MyHUD*)cg::Registry::instance()->get("HUD");
		_e1m = (Enemy1Manager*)cg::Registry::instance()->get("Enemy1Manager");
		_e2m = (Enemy2Manager*)cg::Registry::instance()->get("Enemy2Manager");
	}

	// returns actual level of the game
	int GameManager::getLevel(){
		return actualLevel;
	}

	// loads the entities when loading a new level
	void GameManager::LoadMainEntities() {

		CameraManager* camaraManager = (CameraManager*)cg::Registry::instance()->get("CameraManager");
		camaraManager->init();

		CollisionManager * col = new CollisionManager();
		cg::Registry::instance()->add(col);
		//v->linkEntityAtEnd("CollisionManager");
		col->init(); 

		//____________________CAMERAS_________________________//

		MainCamera * mc = new MainCamera();
		cg::Registry::instance()->add(mc);

		ThirdPersonCamera * tpc = new ThirdPersonCamera("ThirdCamera");
		cg::Registry::instance()->add(tpc);

		FreeCamera * fc = new FreeCamera("FreeCamera");
		cg::Registry::instance()->add(fc);

		v->linkEntityAtEnd("FirstCamera");
		mc->init(); 

		//____________________________________________________//

		Lightning * lightning = new Lightning("Light1");
		cg::Registry::instance()->add(lightning);
		v->linkEntityAtEnd("Light1");
		lightning->init();

		MyWorld * world = new MyWorld();
		cg::Registry::instance()->add(world);
		v->linkEntityAtEnd("World");
		world->init();
		
		Skybox * skybox = new Skybox("MySkybox");
		cg::Registry::instance()->add(skybox);
		v->linkEntityAtEnd("MySkybox");
		skybox->init();

		
		HeightMap * heightmap = new HeightMap();
		cg::Registry::instance()->add(heightmap);
		//v->linkEntityAtEnd("HeightMap");
		//heightmap->init(); 

		
		MyPeanut * peanut = new MyPeanut("Peanut");
		cg::Registry::instance()->add(peanut);
		v->linkEntityAtEnd("Peanut");
		peanut->init();

		MyHUD * hud = new MyHUD("HUD");
		cg::Registry::instance()->add(hud);
		v->linkEntityAtEnd("HUD");
		hud->init(); 

		TreeManager * treeManager = new TreeManager("TreeManager");
		cg::Registry::instance()->add(treeManager);
		v->linkEntityAtEnd("TreeManager");
		treeManager->init();
	}

	// unloads all the entities - when "level to menu" is performed
	void GameManager::UnLoadMainEntities() {

		CameraManager* camaraManager = (CameraManager*)cg::Registry::instance()->get("CameraManager");

		v->unlinkEntity("CollisionManager");
		CollisionManager* col = (CollisionManager*)cg::Registry::instance()->get("CollisionManager");
		cg::Registry::instance()->remove("CollisionManager");
		delete col; 

		if(camaraManager->actualCamera == "FirstCamera") {
			v->unlinkEntity("FirstCamera");
		}
		if(camaraManager->actualCamera == "ThirdCamera") {
			v->unlinkEntity("ThirdCamera");
		}
		if(camaraManager->actualCamera == "FreeCamera") {
			v->unlinkEntity("FreeCamera");
		}
		MainCamera* mainCamera = (MainCamera*)cg::Registry::instance()->get("FirstCamera");
		cg::Registry::instance()->remove("FirstCamera");
		delete mainCamera;

		ThirdPersonCamera* tpCamera = (ThirdPersonCamera*)cg::Registry::instance()->get("ThirdCamera");
		cg::Registry::instance()->remove("ThirdCamera");
		delete tpCamera;

		FreeCamera* fCamera = (FreeCamera*)cg::Registry::instance()->get("FreeCamera");
		cg::Registry::instance()->remove("FreeCamera");
		delete fCamera;

		v->unlinkEntity("Light1");
		Lightning* light = (Lightning*)cg::Registry::instance()->get("Light1");
		cg::Registry::instance()->remove("Light1");
		delete light;

		v->unlinkEntity("World");
		MyWorld* world = (MyWorld*)cg::Registry::instance()->get("World");
		cg::Registry::instance()->remove("World");
		delete world;
		
		v->unlinkEntity("MySkybox");
		Skybox* skybox = (Skybox*)cg::Registry::instance()->get("MySkybox");
		cg::Registry::instance()->remove("MySkybox");
		delete skybox;
		
		//v->unlinkEntity("HeightMap");
		HeightMap* heightmap = (HeightMap*)cg::Registry::instance()->get("HeightMap");
		cg::Registry::instance()->remove("HeightMap");
		delete heightmap; 

		v->unlinkEntity("Peanut");
		MyPeanut* peanut = (MyPeanut*)cg::Registry::instance()->get("Peanut");
		cg::Registry::instance()->remove("Peanut");
		delete peanut;

		v->unlinkEntity("HUD");
		MyHUD* hud = (MyHUD*)cg::Registry::instance()->get("HUD");
		cg::Registry::instance()->remove("HUD");
		delete hud; 

		if(cg::Registry::instance()->exists("Enemy1Manager")) {
			v->unlinkEntity("Enemy1Manager");
			Enemy1Manager* enemy1 = (Enemy1Manager*)cg::Registry::instance()->get("Enemy1Manager");
			cg::Registry::instance()->remove("Enemy1Manager");
			delete enemy1;  
		}

		if(cg::Registry::instance()->exists("Enemy2Manager")) {
			v->unlinkEntity("Enemy2Manager");
			Enemy2Manager* enemy2 = (Enemy2Manager*)cg::Registry::instance()->get("Enemy2Manager");
			cg::Registry::instance()->remove("Enemy2Manager");
			delete enemy2; 
		}

		v->unlinkEntity("CollectableManager");
		CollectableManager* collM = (CollectableManager*)cg::Registry::instance()->get("CollectableManager");
		cg::Registry::instance()->remove("CollectableManager");
		delete collM;

		v->unlinkEntity("TreeManager");
		TreeManager* treeManager = (TreeManager*)cg::Registry::instance()->get("TreeManager");
		cg::Registry::instance()->remove("TreeManager");
		delete treeManager;

	}
		
	// loads the game menu - first time and whenever the menu is called 
	void GameManager::LoadGameMenu() {

		if(actualLevel != 0) {
			UnLoadMainEntities(); // eliminates everything menu doesnt need
		}

		Menu * menu = new Menu("Menu");
		cg::Registry::instance()->add(menu);
		v->linkEntityAtEnd("Menu");

		if(actualLevel != 0) {
			menu->init();
		}

		actualLevel = MENU;
	}


	void GameManager::resetLevel1() {
		_physics.setPosition(13.0f,0.5f,0.0f);
		if(_peanut)
			_peanut->life = 100;
		if(_sm) {
			_sm->woodCounter = 0;
			_sm->rockCounter = 0;
			_sm->wood_weapon = false;
			_sm->rock_weapon = false;
		}
		if(_cm) 
			_cm->init();
		if(_e1m) 
			_e1m->init();
		if(_hud)
			_hud->init();
	}

	void GameManager::resetLevel2() {
		_physics.setPosition(13.0f,0.5f,0.0f);
		if(_peanut)
			_peanut->life = 100;
		if(_sm) {
			_sm->woodCounter = 0;
			_sm->rockCounter = 0;
			_sm->wood_weapon = false;
			_sm->rock_weapon = false;
		}
		if(_cm) 
			_cm->init();
		if(_e2m) 
			_e2m->init();
		if(_hud)
			_hud->init();
	}

	void GameManager::LoadLevel1() {


		UnLoadLevel0(); // remove menu
		resetLevel1();
		LoadMainEntities(); // add the common entities to all levels

		Enemy1Manager * enemy1 = new Enemy1Manager("Enemy1Manager");
		cg::Registry::instance()->add(enemy1);
		v->linkEntityAtEnd("Enemy1Manager");
		enemy1->init();

		CollectableManager * collectablemanager = new CollectableManager("CollectableManager");
		cg::Registry::instance()->add(collectablemanager);
		v->linkEntityAtEnd("CollectableManager");
		collectablemanager->init(); 

		actualLevel = LEVEL1;
	}

	void GameManager::LoadLevel2() {

		UnLoadLevel0(); // remove menu
		resetLevel2();
		LoadMainEntities(); // add the common entities to all levels

		Enemy2Manager * enemy2 = new Enemy2Manager("Enemy2Manager");
		cg::Registry::instance()->add(enemy2);
		v->linkEntityAtEnd("Enemy2Manager");
		enemy2->init();

		CollectableManager * collectablemanager = new CollectableManager("CollectableManager");
		cg::Registry::instance()->add(collectablemanager);
		v->linkEntityAtEnd("CollectableManager");
		collectablemanager->init(); 

		actualLevel = LEVEL2;
	}

	// removes all menu entities
	void GameManager::UnLoadLevel0() {
		v->unlinkEntity("Menu");
		Menu* menu = (Menu*)cg::Registry::instance()->get("Menu");
		cg::Registry::instance()->remove("Menu");
		delete menu;
	}
}